<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>猜数字</title>
</head>
<body>
    <script src="JS/jquery-3.7.1.min.js"></script>
    <button type="button" id="reset">重新开始一局游戏</button>
    <br>
    请输入要猜的数字：<input type="text" id="number">
    <button type="button" id="button">猜</button>
    <br>
    已经猜的次数：<span id="count">0</span>
    <br>
    结果：<span id="result"></span>
    <script>
        $(function() {
            /* 随机生成一个1-100的数字 */
            var guessNumber = Math.floor(Math.random()*100)+1
            var count = 0;
            /* click:点击
               事件驱动 （Event-Drive）：只要真正发生了点击事件时，才执行该函数 */
            $("#button").click(function() {
                count++;
                $("#count").text(count);

                var userGuess = parseInt($("#number").val());
                if(userGuess == guessNumber) {
                    $("#result").text('猜对了');
                    $("#result").css('color','gray');
                }else if(userGuess < guessNumber) {
                    $("#result").text('猜小了');
                    $("#result").css('color','blue');
                }else {
                    $("#result").text('猜大了');
                    $("#result").css('color','red');
                }
            });
            $("#reset").click(function() {
                guessNumber = Math.floor(Math.random()*100)+1
                count = 0;
                $("#count").text(count);
                $("#result").text('');
                $("#number").val("");
            });
        });
    </script>
</body>
</html>